package handler

import (
	"github.com/gin-gonic/gin"
	"main/api/request"
	"main/internal/dao"
	"main/internal/model"
	"net/http"
)

func AddInteraction(c *gin.Context) {
	var req request.AddInteractionReq
	data := model.Interaction{
		UserId:  req.UserId,
		Content: req.Content,
	}
	if !dao.Create(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": data,
	})
}

func ListInteraction(c *gin.Context) {
	var req request.ListInteractionReq

	if !dao.List(int(req.Id), &req) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": req,
	})
}

func UpdateInteraction(c *gin.Context) {
	var req request.UpdateInteractionReq
	data := model.Interaction{
		UserId:  req.UserId,
		Content: req.Content,
	}
	if !dao.Update(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": data,
	})
}

func DelInteraction(c *gin.Context) {
	var req request.DelInteractionReq

	if !dao.Delete(int(req.Id), &req) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": req,
	})
}
